package Utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DBHelp {
    private static Properties properties = new Properties();
    private static DataSource dataSource = null;
    //private static Connection connection =null;
    //存放一个connect
    private static ThreadLocal<Connection> threadLocal = new ThreadLocal();
    static {
        try {
            //使用类加载器找到我们的工作目录
            properties.load(DBHelp.class.getClassLoader().getResourceAsStream("druid.properties"));
            //properties.load(new FileInputStream("D:\\javaWeb\\WebPro\\src\\druid.properties"));
            dataSource = DruidDataSourceFactory.createDataSource(properties);

        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
        }
    }
    //德鲁伊链接
    public static Connection getConnection()  {
        // thread,connection ,获得同一个conenction
        Connection connection = null;
        try {
            connection = threadLocal.get();
            if (connection == null){
                threadLocal.set(dataSource.getConnection());
                connection = threadLocal.get();
            }
            //将链接设置手动提交
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
        }
        return connection;
    }
    public static void CloseALL(ResultSet resultSet, Statement statement,Connection connection){
        try {
            if (resultSet != null){
                resultSet.close();
            }
            if (statement != null){
                statement.close();
            }
            if (connection != null){
                connection.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
        }
    }
    public static void Commit(){
        Connection connection = threadLocal.get();
        if (connection != null){
            try {
                connection.commit();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                CloseALL(null,null,connection);
            }
        }
        // 将threadLost 链接删除，长时间持有该链接，会影响效率
        threadLocal.remove();
    }
    //所谓回滚就是撤销和某个链接相关的操作
    public static void rollback(){
        Connection connection = threadLocal.get();
        if (connection != null){
            try {
                connection.rollback();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                CloseALL(null,null,connection);
            }
        }
        threadLocal.remove();
    }


}
